package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.CnocOsAccess;
import com.soft.idc.common.util.JQPage;

public class AccessManagerController extends Controller {
	public void index() {
		this.render("/perf_kpi_manager/access.html");
	}
	
	public void queryAccess() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		String type_code = this.getPara("type_code");
		sb.append("select a.id, a.type_code, a.ip, a.port, a.snmp_commity, ");
		sb.append("a.url, a.sign, a.state, ");
		sb.append("case(a.state) when '10A' then '在用' when '10P' then '停用' end as state_name, ");
		sb.append("a.create_date, a.modify_date, a.username, a.password ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from cnoc_os_access a where 1=1 ");
		if(type_code !=null ){
			sqlFrom.append(" and a.type_code like '%" + type_code + "%' ");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}
	
	public void add() {
		String type_code = this.getPara("type_code");
		String ip = this.getPara("ip");
		String port = this.getPara("port");
		String snmp_commity = this.getPara("snmp_commity");
		String url = this.getPara("url");
		String sign = this.getPara("sign");
		String state = this.getPara("state");
		String username = this.getPara("username");
		String password = this.getPara("password");
		Date now = new Date();
		CnocOsAccess access = new CnocOsAccess();
		access.setTypeCode(type_code);
		access.setIp(ip);
		if(port!=null && port.length()>0){
			access.setPort(Integer.parseInt(port));
		}
		access.setSnmpCommity(snmp_commity);
		access.setUrl(url);
		access.setSign(sign);
		access.setState(state);
		access.setUsername(username);
		access.setPassword(password);
		access.setCreateDate(now);
		boolean result = access.save();
		this.renderJson(result);
	}
	
	public void delete() {
		final String ids = this.getPara("ids");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] idArr = ids.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from cnoc_os_access where id in(");
				for(int i=0; i<idArr.length; i++){
					if(i==idArr.length-1){
						sb.append(idArr[i] + ")");
					}else{
						sb.append(idArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==idArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		String type_code = this.getPara("type_code");
		String ip = this.getPara("ip");
		String port = this.getPara("port");
		String snmp_commity = this.getPara("snmp_commity");
		String url = this.getPara("url");
		String sign = this.getPara("sign");
		String state = this.getPara("state");
		String username = this.getPara("username");
		String password = this.getPara("password");
		Date now = new Date();
		CnocOsAccess access = new CnocOsAccess();
		access = access.findById(this.getParaToInt("id"));
		access.setTypeCode(type_code);
		access.setIp(ip);
		if(port!=null && port.length()>0){
			access.setPort(Integer.parseInt(port));
		}
		access.setSnmpCommity(snmp_commity);
		access.setUrl(url);
		access.setSign(sign);
		access.setState(state);
		access.setUsername(username);
		access.setPassword(password);
		access.setModifyDate(now);
		boolean result = access.update();
		this.renderJson(result);
	}
}
